package com.kuang.service.user;

import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService{
    //业务层都会调用dao层，所以要引入dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }



    public User login(String userCode, String password) {
        Connection connection=null;
        User user=null;

        try {
            connection= BaseDao.getConnection();

            //通过业务层调用具体的数据库操作

            user=userDao.getLoginUser(connection,userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }
    public boolean updatePwd( int id, String pwd){
        Connection connection=null;
        boolean flag=false;
        //修改密码
        try {
            connection = BaseDao.getConnection();
            if(userDao.updatePwd(connection,id,pwd)>0){
                flag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //查询记录数
    public int getUserCount(String userName,int userRole){
        Connection connection=null;
        int count=0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
          return count;
    }
    //查询用户列表
    public List<User> getUserList(String username, int userRole, int currentPageNo, int pageSize)  {
        ArrayList<User> users = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            users = (ArrayList<User>) userDao.getUserList(connection, username, userRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return users;
    }


   @Test
   public void test2(){
        UserServiceImpl userService=new UserServiceImpl();
       int userCount = userService.getUserCount(null, 3);
       System.out.println(userCount);
   }
    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("admin", "1234567");
        System.out.println(admin.getUserPassword());

    }


}
